package com.sandu.mirrorapp.activity

import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.WindowManager
import android.widget.Toast
import androidx.activity.OnBackPressedCallback
import androidx.lifecycle.Observer
import androidx.navigation.Navigation
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.gyf.immersionbar.ImmersionBar
import com.sandu.lib_common.arouter.ARouterPath
import com.sandu.lib_common.base.BaseDBActivity
import com.sandu.lib_common.base.appViewModel
import com.sandu.lib_common.utils.StatusBarUtil
import com.sandu.lib_mvvm.base.activity.BaseVmVbActivity
import com.sandu.lib_mvvm.network.manager.NetState
import com.sandu.mirrorapp.databinding.ActivityTestBinding
import com.sandu.mirrorapp.viewmodel.state.MainViewModel
import com.tencent.bugly.beta.Beta
import com.sandu.mirrorapp.R

/**
 * 作者　: sandu
 * 时间　: 2024/11/21
 * 描述　: 主页
 */
@Route(path = ARouterPath.MAIN_ACTIVITY)
class MainActivity : BaseDBActivity<MainViewModel, ActivityTestBinding>() {
    var exitTime = 0L
    override fun initView(savedInstanceState: Bundle?) {
        //进入首页检查更新
        Beta.checkUpgrade(false, true)
        onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                val nav = Navigation.findNavController(this@MainActivity, R.id.host_fragment)
                if (nav.currentDestination != null && nav.currentDestination!!.id != R.id.mainfragment) {
                    //如果当前界面不是主页，那么直接调用返回即可
                    nav.navigateUp()
                } else {
                    //是主页
                    if (System.currentTimeMillis() - exitTime > 2000) {
//                        Toast.makeText(this@MainActivity,"test",Toast.LENGTH_LONG).show()
                        ToastUtils.showLong("再按一次退出程序")
                        exitTime = System.currentTimeMillis()
                    } else {
                        finish()
                    }
                }
            }
        })
        appViewModel.appColor.value?.let {
            window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
            supportActionBar?.setBackgroundDrawable(ColorDrawable(it))
//            StatusBarUtil.setColor(this, it, 0)
        }
    }

    override fun showLoading(message: String) {

    }

    override fun dismissLoading() {

    }


    override fun createObserver() {
        appViewModel.appColor.observeInActivity(this, Observer {
            supportActionBar?.setBackgroundDrawable(ColorDrawable(it))
            StatusBarUtil.setColor(this, it, 0)
        })
    }

    /**
     * 示例，在Activity/Fragment中如果想监听网络变化，可重写onNetworkStateChanged该方法
     */
    override fun onNetworkStateChanged(netState: NetState) {
        super.onNetworkStateChanged(netState)
        if (netState.isSuccess) {
            Toast.makeText(applicationContext, "我特么终于有网了啊!", Toast.LENGTH_SHORT).show()
        } else {
            Toast.makeText(applicationContext, "我特么怎么断网了!", Toast.LENGTH_SHORT).show()
        }
    }
}